package com.ldd.mybatis.controller;

import com.ldd.mybatis.model.MyApiResponse;
import com.ldd.mybatis.model.ValidatedUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class ValidatedController {
//    http://localhost:8080/validatedUser?username=johndoe&id=123&password=secret&email=johndoe@example.com

    @RequestMapping(
            method = {RequestMethod.GET},
            value = "/validatedUser"
    )
    public MyApiResponse<ValidatedUser> ValidatedUser(@RequestParam String username,
                                                      @RequestParam String id,
                                                      @RequestParam String password,
                                                      @RequestParam String email) {
        ValidatedUser user = ValidatedUser.builder().username(username).email(email).password(password).id(id).build();
        log.info("ValidatedUser:{}", user);
        return MyApiResponse.success(user);
    }

    //请求地址 http://localhost:8080/postValidatedUser
    // {"username":"johndoe","password":"123456","id":1234,"email":"email@qq.com"}
    @PostMapping(value = "/postValidatedUser")
    public MyApiResponse<ValidatedUser> postValidatedUser(@RequestBody @Validated ValidatedUser user) {
        log.info("postValidatedUser:{}", user);
        return MyApiResponse.success(user);

    }
}
